﻿using System.Collections.Generic;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Fighter.Masters.ModuleData;

namespace Fighter.Masters.Forms
{
    public partial class Titles : ApplicationLibrary.DBManager.DocumentForm
    {
        public Titles()
            : base("Title")
        {
            InitializeComponent();
            keyControl = txtTitle;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarTitlesParam(txtTitle.Text);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerTitle("titleForm", new RadarTitles(), this);
            dbManager.AddMaster(this, FG_Titles.Name, true);
        }

        protected override void OnBindData()
        {
            BindControl(txtTitle, FG_Titles.Title);
            BindControl(txtDescription, FG_Titles.Description);
        }

        protected override void FocusOnNew()
        {
            txtTitle.Focus();
        }

        #endregion
    }

    #region DbManager dbmanagerTitle

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerTitle : DBManager
    {
        public dbmanagerTitle(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new FG_Titles();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(FG_Titles.Title, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter("@p1", FG_Titles.Title);
            sParam.Value = FG_Titles.Title.DefaultValue;
            PList.Add(sParam);
            return PList;
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion
}